package com.iwunu.gateway.service.impl;

import java.util.UUID;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iwunu.gateway.entity.OrgUser;
import com.iwunu.gateway.repository.OrgUserRepository;
import com.iwunu.gateway.service.FlockOrgService;
import com.iwunu.gateway.service.JpaService;
import com.iwunu.gateway.vo.OrgVo;
import com.iwunu.iot.common.util.DateUtil;

@Service
public class FlockOrgServiceImpl implements FlockOrgService{
	  @Autowired
	    private OrgUserRepository orgUserRepository;
	    @Autowired
	    JpaService jpaService;
		public  OrgUser findOrgUser(String phone,String password) {
			OrgUser u = orgUserRepository.findOrgUser(phone,password);
			return u;
		}
		@Override
		public  OrgUser getOrgUserByUserId(String userId) {
			 OrgUser u = orgUserRepository.findOrgUserByUserId(userId);
			return u;
		}

		/**
		 * *保存用户
		 * @param userVo
		 * @return
		 */
		public  OrgUser save(OrgVo orgVo) {
			OrgUser org = new OrgUser();
			String password = new SimpleHash("SHA-1", orgVo.getPhone(), orgVo.getPassword()).toString();	//密码加密
			org.setPassword(password);
			org.setCreateTime(DateUtil.nowDateTime());
			org.setPhone(orgVo.getPhone());
			org.setUserId(UUID.randomUUID().toString());
			OrgUser result = orgUserRepository.save(org);
			return result;
		}
		/**
		 * 
		 * @param org
		 * @param password
		 * @return
		 */
		public OrgUser update( OrgUser org,String password) {
			String pwd = new SimpleHash("SHA-1", org.getPhone(),password).toString();	//密码加密
			org.setPassword(pwd);
			OrgUser result = orgUserRepository.save(org);
			return result;
		}

		@Override
		public void del(String userId) {
			orgUserRepository.deleteById(Long.valueOf(userId));
		}


	  
}
